package com.tem.content.controller;

import com.api.manage.feign.system.SystemClientService;
import com.api.manage.pojo.system.SysBranchOfficeBean;
import com.api.manage.pojo.system.SysDepartmentBean;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.servlet.ModelAndView;
import java.util.Date;
import java.util.List;
import java.util.ArrayList;
import com.tem.common.tool.result.ResultUtil;
import org.springframework.beans.BeanUtils;
import com.tem.content.entity.CmsType;
import com.api.manage.pojo.content.CmsTypeBean;
import com.tem.content.service.ICmsTypeService;
import com.tem.common.core.base.BaseController;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 *  前端控制器
 * </p>
 * @author 陈辉
 * @since 2023-11-07
 */
@RestController
@RequestMapping("/content/cmsType")
public class CmsTypeController extends BaseController{
    @Autowired
    private ICmsTypeService cmsTypeService;

    @Autowired
    private SystemClientService systemClientService;

    @ApiOperation(value = "列表")
    @RequestMapping("list")
    public ResultUtil<IPage<CmsTypeBean>> list(CmsTypeBean request, HttpServletRequest req){
        Long[] myids = this.getMyIds(req);
        if(myids==null || myids.length != 3)return ResultUtil.error("对不起，您没有权限访问。请联系管理员。");
        QueryWrapper<CmsType> wrapper = new QueryWrapper<>();
        if(null != request.getStartTime() && null != request.getEndTime()) {
            wrapper.between("create_time",DateFormatUtils.format(request.getStartTime(),"yyyy-MM-dd HH:mm:ss"),DateFormatUtils.format(request.getEndTime(),"yyyy-MM-dd HH:mm:ss"));
        }
        wrapper.lambda().eq(CmsType::getSysBranchOfficeId , myids[2]);
        wrapper.orderByDesc("seq");
        Page page = new Page<>(request.getCurrent(),3000000);
        IPage listBody = cmsTypeService.page(page,wrapper);
        List<CmsType> entityList=listBody.getRecords();
        if(!entityList.isEmpty()) {
            List<CmsTypeBean> responseList = new ArrayList<>();
            entityList.forEach(it -> {
                CmsTypeBean bean = new CmsTypeBean();
                BeanUtils.copyProperties(it,bean);
                ResultUtil<SysBranchOfficeBean> sdbean = systemClientService.getSysBranchOfficeBeanById(bean.getSysBranchOfficeId());
                bean.setSysBranchOfficeIdName(sdbean.getData().getOfficeName());
                responseList.add(bean);
            });
            List<CmsTypeBean> responseList2 = cmsTypeService.getTree(responseList);
            listBody.setRecords(responseList2);
        }
        return ResultUtil.success(listBody);
    }

    @PostMapping("save")
    @ApiOperation(value = "新增")
    public ResultUtil save(@RequestBody CmsType entity, HttpServletRequest request){
        //entity.setCreateTime(LocalDateTime.now());entity.setDeleted(0);
        if(entity.getCmsTypeId()!=null){
            cmsTypeService.updateById(entity);
        }else{
            entity.setDeleted(0);
            entity.setSysBranchOfficeId(this.getSysBranchOfficeId(request));
            cmsTypeService.save(entity);
        }
        return ResultUtil.success();
    }

    @GetMapping("getById/{id}")
    @ApiOperation(value = "id查询")
    public ResultUtil<CmsTypeBean> getById(@PathVariable String id){
        CmsType entity = cmsTypeService.getById(id);
        if(entity!=null){
            CmsTypeBean bean = new CmsTypeBean();
            BeanUtils.copyProperties(entity,bean);
            return ResultUtil.success(bean);
        }
        return ResultUtil.error("没有找到对象");
    }

    @PutMapping("update")
    @ApiOperation(value = "修改")
    public ResultUtil update(@RequestBody CmsType entity){
        cmsTypeService.updateById(entity);
        return ResultUtil.success();
    }

    @ApiOperation(value = "删除")
    @DeleteMapping("delete/{id}")
    public ResultUtil delete(@PathVariable Long id){
        cmsTypeService.removeById(id);
        return ResultUtil.success();
    }
}